<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="预警类型" prop="warning_type">
        <el-select class="!w-240px" @change="handleQuery" v-model="queryParams.warningType" placeholder="请选择类型"
          style="width: 100%;">
          <el-option v-for="dict in getIntDictOptions(DICT_TYPE.WARNINGTYPE)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['business:warning-config:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button type="danger" plain :disabled="isEmpty(checkedIds)" @click="handleDeleteBatch"
          v-hasPermi="['business:warning-config:delete']">
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>


  <ContentWrap>
    <!-- 按照预警的数据展示 -->
    <el-row v-for="item in list" :key="item.id" class="warning-item-row">
      <el-col :span="24">
        <div class="warning-item">
          <!-- 圆形颜色块 -->

          <span class="color-dot" :style="{ backgroundColor: item.warningColor }"></span>
          <dict-tag :type="DICT_TYPE.WARNINGTYPE" :value="item.warningType" style="margin-right: 20px;" />
          <!-- 文案内容 -->
          <span class="text">
            剩余时间 <strong>&lt; {{ item.warningDays }} 天</strong>，触发
            <span :style="{ color: item.warningColor }">{{ item.warningLevel }}级</span>
            预警，通知 <strong>{{ item.warningUserName }}</strong>
          </span>
        </div>
      </el-col>
    </el-row>

  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table border row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="预警类型" align="center" prop="warningType" width="120px">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.WARNINGTYPE" :value="scope.row.warningType" />
        </template>
      </el-table-column>
      <el-table-column label="预警天数|小时" align="center" prop="warningDays" />
      <el-table-column label="预警等级" align="center" prop="warningLevel" />
      <el-table-column label="预警颜色" align="center" prop="warningColor">
        <template #default="{ row }">
          <span v-if="row.warningColor" class="color-block" :style="{ backgroundColor: row.warningColor }"></span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="预警消息接收人" align="center" prop="warningUserName" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" align="center" width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['business:warning-config:update']">
            编辑
          </el-button>
          <el-button link type="danger" @click="handleDelete(scope.row.id)"
            v-hasPermi="['business:warning-config:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <WarningConfigForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { WarningConfigApi, WarningConfig } from '@/api/business/warningconfig'
import WarningConfigForm from './WarningConfigForm.vue'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
/** 预警配置 列表 */
defineOptions({ name: 'WarningConfig' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<WarningConfig[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  warningType: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WarningConfigApi.getWarningConfigPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await WarningConfigApi.deleteWarningConfig(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 批量删除预警配置 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await WarningConfigApi.deleteWarningConfigList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: WarningConfig[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await WarningConfigApi.exportWarningConfig(queryParams)
    download.excel(data, '预警配置.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>


<style scoped>
.color-block {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1px solid #ddd;
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.1);
}
</style>

<style scoped>
.warning-item-row {
  margin-bottom: 8px;
}

.warning-item {
  padding: 12px 16px;
  border: 1px solid #e8e8e8;
  border-radius: 8px;
  background-color: #fff;
  display: flex;
  align-items: center;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: border-color 0.3s;
}

.warning-item:hover {
  /* border-color: v-bind('item.warningColor'); */
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.color-dot {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  margin-right: 12px;
  box-shadow: 0 0 2px rgba(0, 0, 0, 0.3);
}
</style>
